package com.example.websocket.xml;

import com.example.websocket.xml.bean.TaxCodeModel;
import com.example.websocket.xml.bean.TaxCodeModel;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 解析商品和服务税收编码xml文件数据
 * @author David
 *
 */
public class SaxParseNewTaxCode extends DefaultHandler {

	private List<TaxCodeModel> taxCodes;

	private TaxCodeModel taxCode ;

	private String preTag; // 记录解析上一个几点名称

	private StringBuffer sb;
	
	private Boolean flag = false;
	
	
	public List<TaxCodeModel> getTaxCodes(InputStream xmlStream) {
		SaxParseNewTaxCode handler = new SaxParseNewTaxCode();
		SAXParserFactory factory = SAXParserFactory.newInstance();
		SAXParser parser = null;
		try {
			parser = factory.newSAXParser();
			parser.parse(xmlStream, handler);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (xmlStream != null) {
				try {
					xmlStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}	
			}
			if (factory != null) {
				factory = null;
			}
			if (parser != null) {
				parser = null;
			}
		}
		return handler.getTaxCodes();
	}
	
	public List<TaxCodeModel> getTaxCodes() {
		return taxCodes;
	}

	@Override
	public void startDocument() throws SAXException {
		taxCodes = new ArrayList<>();
	}

	@Override
	public void endDocument() throws SAXException {
		super.endDocument();
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		flag = true;
		if ("BMXX".equals(qName)) {
			taxCode = new TaxCodeModel();
		}
		preTag = qName;
		sb = new StringBuffer();
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		super.endElement(uri, localName, qName);
		flag = false;
		if ("BMXX".equals(qName)) {
			taxCodes.add(taxCode);
		}
		if (preTag != null) {
			String content = sb.toString();
			if ("BB".equals(preTag)) {
				taxCode.setBB(Float.parseFloat(content));
			} else if ("SPBM".equals(preTag)) {
				taxCode.setSPBM(Long.parseLong(content));
			} else if ("SPMC".equals(preTag)) {
				taxCode.setSPMC(content);
			} else if ("SPBMJC".equals(preTag)) {
				taxCode.setSPBMJC(content);
			} else if ("SM".equals(preTag)) {
				taxCode.setSM(content);
			} else if ("ZZSSL".equals(preTag)) {
				taxCode.setZZSSL(content);
			} else if ("GJZ".equals(preTag)) {
				taxCode.setGJZ(content);
			} else if ("HZX".equals(preTag)) {
				taxCode.setHZX(content);
			} else if ("KYZT".equals(preTag)) {
				taxCode.setKYZT(content);
			} else if ("ZZSTSGL".equals(preTag)) {
				taxCode.setZZSTSGL(content);
			} else if ("ZZSZCYJ".equals(preTag)) {
				taxCode.setZZSZCYJ(content);
			} else if ("ZZSTSNRDM".equals(preTag)) {
				taxCode.setZZSTSNRDM(content);
			} else if ("XFSGL".equals(preTag)) {
				taxCode.setXFSGL(content);
			} else if ("XFSZCYJ".equals(preTag)) {
				taxCode.setXFSZCYJ(content);
			} else if ("XFSTSNRDM".equals(preTag)) {
				taxCode.setXFSTSNRDM(content);
			} else if ("TJJBM".equals(preTag)) {
				taxCode.setTJJBM(content);
			} else if ("HGJCKSPPM".equals(preTag)) {
				taxCode.setHGJCKSPPM(content);
			} else if ("PID".equals(preTag)) {
				taxCode.setPID(Long.parseLong(content));
			} else if ("GXSJ".equals(preTag)) {
				taxCode.setGXSJ(content);
			}
		}
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		super.characters(ch, start, length);
		if (!flag) {
			return;
		}
		sb.append(new String(ch, start, length));
	}
	
}
